<?php
class DatacopyAction extends CommonAction {
    /*delete from think_member where site_id = 75;
    delete from think_product_category where site_id = 75;
    delete from think_product where site_id = 75;
    delete from think_member_member where site_id = 75;*/
    //update pid
    public function updatePid() {
        $mdb = D('Member');
        if (!isset($_GET['pages'])) {
            $list = $mdb->where(array(
                'site_id' => __SITEID__
            ))->limit('5')->order('id')->select();
        } else {
            $list = $mdb->where(array(
                'site_id' => __SITEID__
            ))->limit(5 * $_GET['pages'] . ',5')->order('id')->select();
        }
        if (!$list) {
            M('Member')->where(array(
                'id' => 'pid'
            ))->data(array(
                'pid' => 0
            ))->save();
            //更新完成，进入下一步
            $this->success('PID更新成功，进入下一步，准备检查会员关系结构正确性！', U('checkpid'));
            exit;
        }
        foreach ($list as $k => $v) {
            $pid = $mdb->where(array(
                'user_id' => $v['parent_id'],
                'site_id' => __SITEID__
            ))->getField('id');
            //dump($mdb -> getLastSql());
            $mdb->where(array(
                'id' => $v['id']
            ))->data(array(
                'pid' => $pid
            ))->save();
            if (intval($pid) > 0) {
                //$mdb -> member_to_member($v['pid'], $v['id']);
                
            }
        }
        echo '<script>window.location.href="' . U('updatePid', array(
            'pages' => $_GET['pages'] + 1
        )) . '";</script>';
        exit;
    }
    public function getPageNavArr($pid = 0, $arr = array()) {
        $re = M('Member')->where(array(
            'id' => $pid,
            'site_id' => __SITEID__
        ))->find();
        if (in_array($re['pid'], $arr)) {
            M('Member')->where(array(
                'id' => $re['id']
            ))->data(array(
                'pid' => 0
            ))->save();
        }
        if (!is_array($re)) {
            return $arr;
        } else {
            $arr[] = $re['id'];
            return $this->getPageNavArr($re['pid'], $arr);
        }
    }
    public function checkpid() {
        $mdb = D('Member');
        if (!isset($_GET['pages'])) {
            $list = $mdb->where(array(
                'site_id' => __SITEID__
            ))->limit('5')->order('id')->select();
        } else {
            $list = $mdb->where(array(
                'site_id' => __SITEID__
            ))->limit(5 * $_GET['pages'] . ',5')->order('id')->select();
        }
        if (!$list) {
            //更新完成，进入下一步
            $this->success('PID更新成功，进入下一步，恢复会员关系数量！', U('addMember_member'));
            exit;
        }
        foreach ($list as $k => $v) {
            $this->getPageNavArr($v['pid']);
        }
        echo '<script>window.location.href="' . U('checkpid', array(
            'pages' => $_GET['pages'] + 1
        )) . '";</script>';
        exit;
    }
    public function addMember_member() {
        $mmdb = D('Member_member');
        $mdb = D('Member');
        if (!isset($_GET['pages'])) {
            $list = $mdb->where(array(
                'site_id' => __SITEID__
            ))->limit('5')->order('id')->select();
        } else {
            $list = $mdb->where(array(
                'site_id' => __SITEID__
            ))->limit(5 * $_GET['pages'] . ',5')->order('id')->select();
        }
        if (!$list) {
            exit('end');
        }
        foreach ($list as $k => $v) {
            //$pid = $mdb -> where(array('user_id' => $v['parent_id'], 'site_id' => __SITEID__)) -> getField('id');
            //dump($mdb -> getLastSql());
            //$mdb -> where(array('id' => $v['id'])) -> data(array('pid' => $pid)) -> save();
            $mmdb->where(array(
                'smember_id' => $v['id']
            ))->delete();
            if ($v['pid'] > 0) {
                $mdb->member_to_member($v['pid'], $v['id']);
            }
        }
        echo '<script>window.location.href="' . U('addMember_member', array(
            'pages' => $_GET['pages'] + 1
        )) . '";</script>';
        exit;
    }
    public function member_copy() {
        $udb = M();
        $mdb = D('Member');
        if (!isset($_GET['pages'])) {
            $list = $udb->table('ecs_users')->where()->limit('5')->order('user_id')->select();
        } else {
            $list = $udb->table('ecs_users')->where()->limit(5 * $_GET['pages'] . ',5')->order('user_id')->select();
        }
        if (!$list) {
            //更新完成，进入下一步
            $this->success('已经同步数据，进入下一步，准备恢复会员关系！', U('updatePid'));
            exit;
        }
        foreach ($list as $k => $v) {
            $data['site_id'] = __SITEID__;
            //$data['parent_id'] = $pid;
            if ($v['wxid'] == '') {
                continue;
            }
            $wret = $udb->table('wxch_user')->where(array(
                'wxid' => $v['wxid']
            ))->find();
            $data['openid'] = $wret['wxid'];
            $data['nickname'] = $wret['nickname'];
            $data['img_url'] = $wret['headimgurl'];
            $data['is_subscribe'] = $wret['subscribe']; 
            $data['level'] = $v['isdongjia'];
            $data['user_id'] = $v['user_id'];
			$data['leveldown'] = 0;
            $data['parent_id'] = $v['parent_id'];
            $mdb->data($data)->add();
        }
        echo '<script>window.location.href="' . U('member_copy', array(
            'pages' => $_GET['pages'] + 1
        )) . '";</script>';
        exit;
    }

    public function copyproduct(){
        D('Product_category') -> copy1();
    }
}

